package org.btik.light.mobile.common.handler.nbi;

import android.view.View;
import org.btik.context.SimpleContext;
import org.btik.light.meta.base.video.VideoChannelType;
import org.btik.light.mobile.common.handler.DevBinder;
import org.btik.light.mobile.meta.mo.DefaultDevMo;
import org.btik.light.mobile.platform.util.HttpUtil;
import org.btik.light.tool.ByteUtil;
import org.btik.light.tool.type.StringUtil;
import org.btik.video.server.VideoServer;

import java.io.Serializable;
import java.util.function.Consumer;

import static org.btik.light.meta.LightDevAttrConstant.CHANNEL_INDEX;
import static org.btik.light.mobile.platform.conf.ContextGlobalNames.VIDEO_SERVER;
import static org.btik.light.mobile.platform.util.HttpConstant.*;

/**
 * @author lustre
 * @since 2023/5/1 23:38
 */
public class NbiVideoOnController implements Consumer<View>, DevBinder {
    private final RawNbiCommand rawNbiCommand;

    private AttachVideoChannelDto attachVideoChannelDto;

    public NbiVideoOnController(byte[] rawNbiCommand) {
        this.rawNbiCommand = new RawNbiCommand(rawNbiCommand);
    }

    @Override
    public void accept(View view) {
        String nbiHost = NbiAgent.getNbiHost();
        // 服务端当前摄像头切换通道至 udp通道
        HttpUtil.doPost(String.format(URL_BASE, nbiHost, ATTACH_VIDEO_CHANNEL), attachVideoChannelDto, this::onHttpResponse);


    }

    private void onHttpResponse(HttpUtil.HttpResponse httpResponse) {
        if (!httpResponse.isSuccess()) {
            String error = httpResponse.getError();
            if (StringUtil.isEmpty(error)) {
                error = httpResponse.getBody();

            }
            System.out.println("code:" + httpResponse.getCode());
            System.out.println();
            System.out.println(error);
            return;
        }
        // 打开视频从服务端到当前app的通路
        VideoServer videoServer = SimpleContext.get(VIDEO_SERVER);
        videoServer.helloPushServer(ByteUtil.u8StrToU8Array(attachVideoChannelDto.getDevSn()),
                NbiAgent.getNbiHost());
        // 最后才下发命令给设备发送开始传图
        NbiAgent.send(rawNbiCommand);

    }

    @Override
    public void bind(DefaultDevMo devMo) {
        String sn = devMo.getSn();
        rawNbiCommand.setTarget(sn);
        attachVideoChannelDto = new AttachVideoChannelDto();
        attachVideoChannelDto.setDevSn(sn);
        attachVideoChannelDto.setChannelType(VideoChannelType.RAW_UDP.ordinal());
        String attr = devMo.getAttr(CHANNEL_INDEX);
        try {
            int channelIndex = Integer.parseInt(attr);
            attachVideoChannelDto.setChannelIndex(channelIndex);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class AttachVideoChannelDto implements Serializable {
        private int channelType;
        private int channelIndex;
        private String devSn;

        public int getChannelType() {
            return channelType;
        }

        public void setChannelType(int channelType) {
            this.channelType = channelType;
        }

        public int getChannelIndex() {
            return channelIndex;
        }

        public void setChannelIndex(int channelIndex) {
            this.channelIndex = channelIndex;
        }

        public String getDevSn() {
            return devSn;
        }

        public void setDevSn(String devSn) {
            this.devSn = devSn;
        }
    }
}
